package com.sdcqjy.property.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.widget.SpacesItemDecoration;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.AttenListAdapter;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.AttenMode;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.AttenListPresenter;
import com.sdcqjy.property.presenter.contract.AttenListContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.sdcqjy.property.MyConstants.ActionAttenRef;

/**
 * Created by 磁磁帅 on 2018/4/30.
 * 功能：关注和阅读
 */
public class AttenListActivity extends SimpleBaseActivity<AttenListPresenter> implements AttenListContract.View, AttenListAdapter.OnAdapterClickListener {
    @BindView(R.id.textTitle)
    TextView textTitle;
    @BindView(R.id.listView)
    RecyclerView listView;
    private List<AttenMode> modeList;
    private AttenListAdapter adapter;
    private int type;
    private boolean isRef = false;
    private BroadcastReceiver receiver;

    //0关注，1阅读
    public static void open(Context context, int type) {
        Intent intent = new Intent(context, AttenListActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_msg);
        ButterKnife.bind(this);
        presenter = new AttenListPresenter(this);
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        textTitle.setText(type == 0 ? R.string.wodeguanzhu : R.string.wodeyuedu);

        modeList = new ArrayList<>();
        adapter = new AttenListAdapter(getActivity(), modeList).setOnAdapterClickListener(this);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listView.addItemDecoration(new SpacesItemDecoration(OtherUtils.dip2px(getActivity(), 10), new boolean[]{false, false, false, true}));

        if (type == 0) regRece();
    }

    protected void asynRun() {
        presenter.getList(type);
    }

    private void regRece() {
        receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                isRef = true;
            }
        };
        IntentFilter filter = new IntentFilter(ActionAttenRef);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
    }

    protected void onResume() {
        super.onResume();
        if (isRef) {
            isRef = false;
            asynRun();
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
        }
    }

    public void getAttenRet(List<AttenMode> data) {
        if (data == null) data = new ArrayList<>();
        modeList.addAll(data);
        adapter.notifyDataSetChanged();
    }

    public void getDetailsModeRet(ListMsgMode data) {
        if (type == 0) {
            data.isAttention = 1;
        }
        data.isRead = 1;
        DetailsActivity.open(getActivity(), data);
    }

    public void onAdapterClicked(AttenMode mode) {
        presenter.getDetailsMode(mode.documentID + "");
    }
}
